/*
 * TestCallStack.cpp
 *
 *  Created on: 2010-12-14
 *      Author: Xairy
 */

#include "TestCallStack.h"
#include "TStringItem.h"

_LIT(KStackName, "StackUnit");
void TestCallStack::setUp()
{
    iStack.CreateL(KStackName, 1024*4);
}

void TestCallStack::tearDown()
{
    iStack.Close();
}

_LIT(aString, "abcde");
_LIT(aTestString2, "abcdef");
void TestCallStack::testStackOperation()
{
   
   TStringItem* oneItem = TStringItem::NewL(_L("a"));
   TStringItem* twoItem = TStringItem::NewL(_L("ab"));
   TStringItem* threeItem = TStringItem::NewL(_L("abc"));
   TStringItem* fourItem = TStringItem::NewL(_L("abcd"));
   CleanupDeletePushL(oneItem);
   CleanupDeletePushL(twoItem);
   CleanupDeletePushL(threeItem);
   CleanupDeletePushL(fourItem);
   
   iStack.Push(*oneItem);
   TS_ASSERT_EQUALS(iStack.Count(), 1);
   TS_ASSERT(!iStack.isEmpty());
   
   iStack.Push(*twoItem);
   TS_ASSERT_EQUALS(iStack.Count(), 2);
   TS_ASSERT(!iStack.isEmpty());
   
   iStack.Push(*threeItem);
   TS_ASSERT(!iStack.isEmpty());
   TS_ASSERT_EQUALS(iStack.Count(), 3);
   
   iStack.Push(*fourItem);
   TS_ASSERT(!iStack.isEmpty());
   TS_ASSERT_EQUALS(iStack.Count(), 4);
   
   iStack.Push(TStringItemType, (TUint16)(2*aString().Length()), aString().Ptr());
   TS_ASSERT(iStack.Top().Type() == TStringItemType);
   TS_ASSERT(iStack.Top().Size() == (sizeof(TItem) + 2 * aString().Length()));
   const TStringItem& topStringItem = static_cast<const TStringItem&>(iStack.Top());
   TDesC* topString = topStringItem.toString();
   TS_ASSERT_EQUALS_DESCRIPTOR(*topString, aString());
   delete topString;
   TS_ASSERT_EQUALS(iStack.Count(), 5);
   
   iStack.Push(TStringItemType, (TUint16)(2*aTestString2().Length()), aTestString2().Ptr());
   TS_ASSERT(iStack.Top().Type() == TStringItemType);
   TS_ASSERT(iStack.Top().Size() == (sizeof(TItem) + 2 * aTestString2().Length()));
   const TStringItem& topStringItem2 = static_cast<const TStringItem&>(iStack.Top());
   TDesC* topString2 = topStringItem2.toString();
   TS_ASSERT_EQUALS_DESCRIPTOR(*topString2, aTestString2());
   delete topString2;
   TS_ASSERT_EQUALS(iStack.Count(), 6);
   
   iStack.Push(*oneItem);
   TS_ASSERT_EQUALS(iStack.Count(), 7);
   TS_ASSERT(oneItem->Equal(iStack.Top()));
   
   TDesC* dumpString = iStack.DumpL();
   RDebug::RawPrint(*dumpString);
   delete dumpString;
   
   iStack.Pop();
   const TStringItem& topStringItem3 = static_cast<const TStringItem&>(iStack.Top());
   TDesC* topString3 = topStringItem3.toString();
   TS_ASSERT_EQUALS_DESCRIPTOR(*topString3, aTestString2());
   delete topString3;
   iStack.Pop();
   TS_ASSERT_EQUALS(iStack.Count(), 5);
      
   iStack.Pop();
   TS_ASSERT_EQUALS(iStack.Count(), 4);
   TS_ASSERT(!iStack.isEmpty());
   
   TS_ASSERT(fourItem->Equal(iStack.Top()));
   iStack.Pop();
   TS_ASSERT_EQUALS(iStack.Count(), 3);
   TS_ASSERT(!iStack.isEmpty());
   
   TS_ASSERT(threeItem->Equal(iStack.Top()));
   iStack.Pop();
   TS_ASSERT_EQUALS(iStack.Count(), 2);
   TS_ASSERT(!iStack.isEmpty());
   
   TS_ASSERT(twoItem->Equal(iStack.Top()));
   iStack.Pop();
   TS_ASSERT_EQUALS(iStack.Count(), 1);
   TS_ASSERT(!iStack.isEmpty());
   
   TS_ASSERT(oneItem->Equal(iStack.Top()));
   iStack.Pop();
   TS_ASSERT_EQUALS(iStack.Count(), 0);
   TS_ASSERT(iStack.isEmpty());
   
   CleanupStack::PopAndDestroy(4, oneItem);
}

void TestCallStack::testStackOverflow()
{
}
